﻿using System;
using System.Collections.Generic;
using System.Text;
using HomeDMSDomain;

namespace bind_n_find
{
    /// <summary>
    /// Helper class operating with string array 
    /// (can be used for dropdown combo with dropdown string list updated according 
    /// to string entered in combo box)
    /// </summary>
    class string_array_search_helper
    {
        private List<string> _array = new List<string>();

        /// <summary>
        /// Constuction from List of strings
        /// </summary>
        /// <param name="array"></param>
        public string_array_search_helper(List<string> array)
        {
            _array = array;
        }

        /// <summary>
        /// Construction from string array
        /// </summary>
        /// <param name="array"></param>
        public string_array_search_helper(string[] array)
        {
            try
            {
                foreach (string str in array)
                {
                    _array.Add(str);
                }
            }
            catch (Exception ex)
            {
                BNFTrace.DoTrace(TraceTypes.Error, string.Format("Exception in 'string_array_search_helper.string_array_search_helper': {0}", ex.Message));

            }
        }

        /// <summary>
        /// Construction from linq table 
        /// </summary>
        /// <param name="Tags"></param>
        public string_array_search_helper(System.Data.Linq.Table<Tag> Tags)
        {
            try
            {
                foreach (Tag tag in Tags)
                {
                    _array.Add(tag.Name);
                }
            }
            catch (Exception ex)
            {
                BNFTrace.DoTrace(TraceTypes.Error, string.Format("Exception in 'string_array_search_helper.string_array_search_helper': {0}", ex.Message));

            }

        }

        /// <summary>
        /// Updates dropdown array according to current string pattern
        /// </summary>
        /// <param name="new_search_string"></param>
        /// <returns></returns>
        public List<string> update_search(string new_search_string)
        {
            if (string.IsNullOrEmpty(new_search_string))
                return _array;
            List<string> new_array = new List<string>();

            try
            {
                foreach (string str in _array)
                {
                    if (str.ToLower().Contains(new_search_string.ToLower()))
                    {
                        new_array.Add(str);
                    }
                }
            }
            catch (Exception ex)
            {
                BNFTrace.DoTrace(TraceTypes.Error, string.Format("Exception in 'string_array_search_helper.update_search': {0}", ex.Message));

            }
            return new_array;
        }

        /// <summary>
        /// Case insensitive check if array contains the string
        /// </summary>
        /// <param name="array"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        private bool _lower_contains(List<string> array, string str)
        {
            try
            {
                string low_str = str.ToLower();
                foreach (string astr in array)
                {
                    if (astr.ToLower() == low_str)
                        return true;
                }
            }
            catch (Exception ex)
            {
                BNFTrace.DoTrace(TraceTypes.Error, string.Format("Exception in 'string_array_search_helper._lower_contains': {0}", ex.Message));

            }
            return false;
        }

        /// <summary>
        /// Add new string to the array
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public List<string> add_string(string str)
        {
            try
            {
                if (!_lower_contains(_array, str))
                {
                    _array.Add(str);
                }
                else
                {
                    throw new Exception(string.Format(Properties.Resources.Tag_Exists_Exception, str));
                }
            }
            catch (Exception ex)
            {
                BNFTrace.DoTrace(TraceTypes.Error, string.Format("Exception in 'string_array_search_helper.add_string': {0}", ex.Message));

            }
            return _array;
        }

        /// <summary>
        /// Delete strings from the array
        /// </summary>
        /// <param name="str_array"></param>
        /// <returns></returns>
        public List<string> delete_strings(string[] str_array)
        {
            try
            {
                foreach (string str in str_array)
                {
                    if (_array.Contains(str))
                        _array.Remove(str);
                }
            }
            catch (Exception ex)
            {
                BNFTrace.DoTrace(TraceTypes.Error, string.Format("Exception in 'string_array_search_helper.delete_strings': {0}", ex.Message));

            }

            return _array;
        }

    }
}
